/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: StandardController
 * @Package com.rx.core.bean
 * @Description: 标准控制器
 * @author: 陈锦韬
 * @date: 2020\8\12 0012
 * @version V1.0
 * @Copyright: 2020 陈锦韬  All rights reserved.
 */
package com.rx.stang.controller;

import com.rx.core.base.RxBaseConstant;
import com.rx.core.base.RxBaseController;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.enm.RX_STATUS;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import com.rx.stang.bean.RxMainConfigBean;
import com.rx.stang.vo.*;
import com.rx.stang.vo.relation.DoubleTangNousInfo;
import feign.Param;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: Administrator
 * @Description: 标准控制器
 * @date: 2020\8\12 0012
 */
@RestController
@RequestMapping(RxBaseConstant.CONFIG_COMMON_API)
@Slf4j
public class STangCommonController extends RxBaseController {

    @Autowired
    com.rx.stang.service.STangCommonService commonService;
    @Autowired
    RxMainConfigBean rxMainConfigBean;

    /**
     * 千古风流人物c
     * @return
     */
    @PostMapping(value = "external-person")
    public Object externalPerson() throws IllegalAccessException, InstantiationException {
        return commonService.externalPerson();
    }

    /**
     * 古今多少事
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @PostMapping(value = "external-event")
    public Object externalEvent() throws IllegalAccessException, InstantiationException {
        return commonService.externalEvent();
    }
    /**
     * 诗三首
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @PostMapping(value = "external-poetry")
    public Object externalPoetry() throws IllegalAccessException, InstantiationException {
        return commonService.externalPoetry();
    }
    /**
    *  年轮查询
    * */
    @PostMapping(value = "external-annual-tag")
    public Object externalAnnualTag(@RequestBody DoubleTangNousInfo param) throws IllegalAccessException, InstantiationException {
        return commonService.externalAnnualTag(param);
    }

    @PostMapping(value = "latest-colmunist")
    public Object latestColmunist() throws IllegalAccessException, InstantiationException {
        return commonService.latestColmunist();
    }

    /**
     * 蒙学
      */
    @PostMapping(value = "rudimentary-book")
    public Object rudimentaryBook(@RequestBody TangBookInfo param) throws IllegalAccessException, InstantiationException {
        return commonService.rudimentaryBook(param);
    }
    /**
     * 蒙学
     */
    @PostMapping(value = "article-page")
    public Object articlePage(@RequestBody TangArticleInfo param) throws IllegalAccessException, InstantiationException {
        return commonService.articlePage(param);
    }
    @PostMapping(value = "book-page")
    public Object bookPage(@RequestBody TangBookInfo param) throws IllegalAccessException, InstantiationException {
        return commonService.bookPage(param);
    }
    /**
     *  评论查询
     * */
    @PostMapping(value = "book-comment")
    public Object bookComment(@RequestBody TangCommentInfo param) throws IllegalAccessException, InstantiationException {
        return commonService.bookComment(param);
    }

    /**
     * 盖章查询
     * @param param
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @PostMapping(value = "book-seal")
    public Object bookSeal(@RequestBody TangSealInfo param) throws IllegalAccessException, InstantiationException {
        return commonService.bookSeal(param);
    }
    @PostMapping(value = "book-notes")
    public Object bookSeal(@RequestBody TangNotesInfo param) throws IllegalAccessException, InstantiationException {
        return commonService.bookNotes(param);
    }
    @PostMapping("/hello")
    public MainHelloVo hello(@RequestBody MainHelloVo mainHelloVo){
        return  commonService.hello(mainHelloVo);
    }


    @RequestMapping("/download")
    public void download(@RequestParam String fileName, @RequestParam String sequences, HttpServletRequest request, HttpServletResponse response) {

        SimpleDateFormat formatter = new SimpleDateFormat(RxBaseConstant.DATE_FORMAT_DATETIME.trim());
        String dateString = formatter.format(new Date()).trim();
        String md5 = RxUtil.EncryptUtil.toMD5(dateString.substring(0,16));
        if(!md5.equalsIgnoreCase(sequences)){
            // throw new RxBaseException("999999","错误");
        }

        commonService.downloadFile(response, rxMainConfigBean.getFilePath(),fileName, !request.getHeader("User-Agent").contains("Trident"));
    }

    @ResponseBody
    @RequestMapping("/columnist-page")
    public Object columnistPage(@Valid @RequestBody ColumnistInfoVo data) throws IllegalAccessException, InstantiationException {
        return commonService.columnistPage(data);
    }


    @RequestMapping("/image-download")
    public void imageDownload(@RequestParam String fileName, @RequestParam String fileType, HttpServletRequest request, HttpServletResponse response) {

        if(EmptyChecker.notEmpty(fileType)){
            return;
        }

        RxUtil.CheckUtil.check(fileName, RESULT_CODE.EMPTY,"文件名");
        String date = "";
        if(fileName.contains(RxBaseConstant.LOCAL_FILE_PREFIX)){
            String[] fileNames = fileName.split("_");
            date = fileNames[1] + "/";
        }



        commonService.downloadFile(response,rxMainConfigBean.getImagePath() + date, fileName, !request.getHeader("User-Agent").contains("Trident"));
    }

    @PostMapping("image-upload")
    public Object upload(@RequestParam("file") MultipartFile parameterFilename) {
        return commonService.upload(parameterFilename);
    }

}
